<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends AuthController {

   public function _initialize(){
         
		 parent::_initialize();
    }

    public function index(){
	    echo "hello world!";
        $this->display();
    }
	
	 public function admin_list(){
	    $user2 = M("user2");
		
		import("ORG.Util.Page");// 导入分页类
		$count = $user2->count();
		$page = new \Think\Page($count,10);//tp3.2新增
		$show = $page->show();
		$limit = $page->firstRow.','.$page->listRows;
		
		$admin_list= $user2 -> order("id desc") -> limit($limit)->select();
		$this->assign(array(
		    'admin_list' => $admin_list,
			'page' => $show,
		));
        $this->display();
    }
	
	/*管理员内容编辑*/
	public function admin_edit(){
	    $user2 = M("user2");
	    $id = I('get.id');
		$where = array('id' => $id);
		if (is_numeric($id)){
		     $user2_info = $user2 ->where($where) -> order("id desc") ->find();
		}else{
		   $this->error("参数传递错误");
		}
        //print_r($user2_info);
		$this->assign('info',$user2_info);
        $this->display();
    }
	
	/*管理员信息更新*/
	public function admin_update(){
	   $id = I('get.id');
	   $user2 = M("user2");
	   if (!isset($id) || $id ==""){//id为空表示新增
	      $data['username'] = I('post.username');
		  $data['password'] = md5(I('post.password'));
		  $data['password2'] = md5(I('post.password2'));
		  //$data['password'] = md5(I('post.password')) ? md5(I('post.password2')): md5(I('post.password'))
		  $data['email'] = I('post.email');
		  $data['admintype'] = I('post.admintype');
		  $data['addtime'] = I('post.addtime');
		  $count = $user2 -> add($data);
		  if($count){
		      $this->success("添加成功",U('User/admin_list'));
		  }else{
			  $this->error("添加失败");	
		  }
		  
	   }else{
		   if (IS_POST){
		       $admin  = $user2 ->where(array('id'  =>$id)) ->find();
	           if(intval($admin['admintype']) < intval($_SESSION['Aadmintype'])){
	               $this->error("您无权修改");
	           }else{
				   $user2->username = I('post.username');
				   $user2->password = trim(md5(I('post.password2'))) ? md5(I('post.password2')): md5(I('post.password'));
				   $user2->email = I('post.email');
				   $user2->admintype = I('post.admintype');
				   $where['id'] = $id;
				   $count = $user2 ->where($where) -> save();
				   if($count!== false){
					   $this->success("修改成功",U('User/admin_list'));
				   }else{
						$this->error("修改失败");	
				   }
			   }
			}else{
			   $this->error("参数传递失败");	
		   }
	   
	   }//修改
	}
	
	/*管理员信息添加页面*/
	public function admin_add(){
	    $now = time();
		$this->assign('now',$now);
	    $this->display();
	}
	
	/*管理员信息验证*/
	/*
	   status = 0 表示非法字符
	   status = 1 表示为空
	   status = 2 表示已存在
	   status = 3 表示正常
	*/
	public function admin_ajax(){
	    $username = I("post.username");
		$user2 = M("user2");
		if (!preg_match("/^[A-Za-z0-9_@#$*%]+$/",$username)){
		    echo "0";//非法
		}else{
			if (isset($username) || $username <>""){
				$where['username'] = $username;
				$data = $user2 -> where($where) ->find();
				if(empty($data)){
					echo "3";
				}else{
					echo '2';//y已存在
				}
			}else{
			   echo "1";//为空
			}
		}//合法
	}
	
	/*管理员删除*/
	public function admin_deladmin(){
	     $id = I('get.id');//单条
		 $ids = I('post.id');//批量
		 $ids = join(',', $ids);
		 $user2 = M("user2");
		if (!empty($id) && empty($ids) && isset($id)){
		    $where['id'] = $id;
			$count = $user2->where($where)->delete();
		}else{
			$count = $user2->where(array('id' => array('in', $ids)))->delete();
		}
		if($count){
		    $this->success("删除成功",U('User/admin_list'));
		}else{
		    $this->error("删除失败");	
		}
	}
	
	
	/*登录日志*/
	public function admin_log(){
	    $log = M("log");
		
		import("ORG.Util.Page");// 导入分页类
		$count = $log->count();
		$page = new \Think\Page($count,20);//tp3.2新增
		$show = $page->show();
		$limit = $page->firstRow.','.$page->listRows;
		
		$logs= $log -> order("id desc") -> limit($limit)->select();
		$this->assign(array(
		    'logs' => $logs,
			'page' => $show,
		));
        $this->display();
	}
	
	/*日志删除*/
	public function admin_dellog(){
	     $id = I('get.id');//单条
		 $ids = I('post.id');//批量
		 $ids = join(',', $ids);
		 $log = M("log");
		if (!empty($id) && empty($ids) && isset($id)){
		    $where['id'] = $id;
			$count = $log->where($where)->delete();
		}else{
			$count = $log->where(array('id' => array('in', $ids)))->delete();
		}
		if($count){
		    $this->success("删除成功",U('User/admin_log'));
		}else{
		    $this->error("删除失败");	
		}
	}
	
}